
		/* ...霸屏事件.start */
		$(".J__wchatBp").on("click", function(){
			var bpidx = wcPop({
				skin: 'ios',
				content: $("#J__popupTmpl-Baping").html(),
				style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
				shadeClose: false,

				btns: [
					{
						text: '我要霸屏',
						style: 'background:#00c1de;color:#fff;font-size:16px;',
						onTap() {
							alert("霸屏成功！");
							wcPop.close(bpidx);
						}
					}
				]
			});
		});
		//霸屏时长picker
		$("body").on("click", ".wc__popupTmpl input[name='bpTimeline']", function(){
			var that = $(this);
			weui.picker([{
					label: '10s　￥8',
					value: 8
				},{
					label: '20s　￥16',
					value: 16
				},{
					label: '30s　￥24',
					value: 24
				},{
					label: '40s　￥32',
					value: 32
				},{
					label: '50s　￥40',
					value: 40
				},{
					label: '60s　￥48',
					value: 48
				}], {
					onChange: function(res){
						console.log(res);
					},
					onConfirm: function(res){
						console.log(res);
						that.val("￥" + res);
					}
				}
			);
		});
		/* ...霸屏事件.end */

		/* ...打赏事件.start */
		$(".J__wchatDs").on("click", function(){
			var bpidx = wcPop({
				skin: 'ios',
				content: $("#J__popupTmpl-Dashang").html(),
				style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
				shadeClose: false,

				btns: [
					{
						text: '<span class="btn-dashang">打赏</span>',
						style: 'background:#00c1de;color:#fff;font-size:16px;',
						onTap() {
							alert("打赏成功！");
							wcPop.close(bpidx);
						}
					}
				]
			});
		});
		//打赏节目picker
		$("body").on("click", ".wc__popupTmpl input[name='dschooseProgram']", function(){
			var that = $(this);
			weui.picker([{
					label: '小品：送礼',
					value: '小品：送礼'
				},{
					label: '歌曲：红日',
					value: '歌曲：红日'
				},{
					label: '相声：就服你',
					value: '相声：就服你'
				},{
					label: '歌曲：上海滩',
					value: '歌曲：上海滩'
				},{
					label: '小品：回家过年',
					value: '小品：回家过年'
				},{
					label: '相声：逗你玩',
					value: '相声：逗你玩'
				}], {
					onChange: function(res){
						console.log(res);
					},
					onConfirm: function(res){
						console.log(res);
						that.val(res);
					}
				}
			);
		});
		//选择礼物
		$("body").on("click", "#J__chooseGift .gift", function(){
			$(this).addClass("selected").siblings().removeClass("selected");
			$(".popui__panel-btn .btn-dashang").html('支付 <em class="ff-ar">￥<i class="moneyNum">'+ $(this).find(".amount em").text() +'</i></em> 打赏');
			
			console.log($(this).attr("data-gift"));
		});
		/* ...打赏事件.end */

		/* ...红包事件.start */
		$(".J__wchatHb").on("click", function(){
			var bpidx = wcPop({
				skin: 'ios',
				content: $("#J__popupTmpl-Hongbao").html(),
				style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
				shadeClose: false,

				btns: [
					{
						text: '塞钱进红包',
						style: 'background:#00c1de;color:#fff;font-size:16px;',
						onTap() {
							alert("塞钱成功！");
							wcPop.close(bpidx);
						}
					}
				]
			});
		});
		/* ...红包事件.end */

		// ...关闭
		$("body").on("click", ".wc__popupTmpl .wc-xclose", function(){
			wcPop.close();
		});



	
	
		/** __公共函数 */
		$(function(){
			// 禁止长按弹出系统菜单
			/**
			$(".wechat__panel").on("contextmenu", function(e){
				e.preventDefault();
			});
			**/
		});
		
		/** __自定函数 */
		$(function(){
			// ...滚动聊天区底部
			function wchat_ToBottom(){
				//$(".wc__chatMsg-panel").animate({scrollTop: $("#J__chatMsgList").height()}, 0);
				$('.wc__chatMsg-panel').scrollTop($('#J__chatMsgList')[0].scrollHeight);
			}

			// ...点击聊天面板区域
			$(document).on("click", ".wc__chatMsg-panel", function(e){
				var _tapMenu = $(".wc__chatTapMenu");
				if(_tapMenu.length && e.target != _tapMenu && !$.contains(_tapMenu[0], e.target)){
					// 关闭长按菜单
					_tapMenu.hide();
					$(".wc__chatMsg-panel").find("li .msg").removeClass("taped");
				}
				$(".wc__choose-panel").hide();
			});

			// ...表情、选择区切换
			$(".wc__editor-panel").on("click", ".btn", function(){
				var that = $(this);
				$(".wc__choose-panel").show();

				if (that.hasClass("btn-emotion")) {
					$(".wc__choose-panel .wrap-emotion").show();
					$(".wc__choose-panel .wrap-choose").hide();
					// 初始化swiper表情
					!emotionSwiper && $("#J__emotionFootTab ul li.cur").trigger("click");
				} else if (that.hasClass("btn-choose")) {
					$(".wc__choose-panel .wrap-emotion").hide();
					$(".wc__choose-panel .wrap-choose").show();
				}
				wchat_ToBottom();
			});

			// ...处理编辑器信息
			var $editor = $(".J__wcEditor"), _editor = $editor[0];
			function surrounds(){
				setTimeout(function () { //chrome
					var sel = window.getSelection();
					var anchorNode = sel.anchorNode;
					if (!anchorNode) return;
					if (sel.anchorNode === _editor ||
						(sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === _editor)) {
						
						var range = sel.getRangeAt(0);
						var p = document.createElement("p");
						range.surroundContents(p);
						range.selectNodeContents(p);
						range.insertNode(document.createElement("br")); //chrome
						sel.collapse(p, 0);
						
						(function clearBr() {
							var elems = [].slice.call(_editor.children);
							for (var i = 0, len = elems.length; i < len; i++) {
								var el = elems[i];
								if (el.tagName.toLowerCase() == "br") {
									_editor.removeChild(el);
								}
							}
							elems.length = 0;
						})();
					}
				}, 10);
			}
			// 格式化编辑器包含标签
			_editor.addEventListener("click", function () {
				$(".wc__choose-panel").hide();
			}, true);
			_editor.addEventListener("focus", function(){
				surrounds();
			}, true);
			_editor.addEventListener("input", function(){
				surrounds();
			}, false);
			// 点击表情
			$("#J__swiperEmotion").on("click", ".face-list span img", function(){
				var that = $(this), range;

				if(that.hasClass("face")){ //小表情
					var img = that[0].cloneNode(true);
					_editor.focus();
					_editor.blur(); //输入表情时禁止输入法

					setTimeout(function(){
						if(document.selection && document.selection.createRange){
							document.selection.createRange().pasteHTML(img);
						}else if(window.getSelection && window.getSelection().getRangeAt){
							range = window.getSelection().getRangeAt(0);
							range.insertNode(img);
							range.collapse(false);

							var sel = window.getSelection();
							sel.removeAllRanges();
							sel.addRange(range);
						}
					}, 10);
				}else if(that.hasClass("del")){ //删除
					_editor.focus();
					_editor.blur(); //输入表情时禁止输入法

					setTimeout(function(){
						range = window.getSelection().getRangeAt(0);
						range.collapse(false);

						var sel = window.getSelection();
						sel.removeAllRanges();
						sel.addRange(range);
						document.execCommand("delete");
					}, 10);
				} else if(that.hasClass("lg-face")){ //大表情
					var _img = that.parent().html();
					//记录消息
				$.post("/chat/group", { id: ljc_id, content: _img,fid:ljc_fid },
				function(data){
					//消息回调
					if(data=="s1"){
						Tmsg('聊天室当前禁言不能发言',3);
						return false;
					}else if(data=="s2"){
						Tmsg('您已经被踢出此聊天室',3);
						window.location.href = '/chat';
						return false;
					}else if(data=="s9"){
					    Tmsg('您发言过于频繁了',3);
						return false;
					}
					});
					var _tpl = [
						'<li class="me">\
							<div class="content">\
								<p class="author">'+ ljc_name +'</p>\
								<div class="msg lgface">'+ _img + '</div>\
							</div>\
							<a class="avatar" href="/chat/fdinfo"><img src="'+ ljc_tx +'" /></a>\
						</li>'
					].join("");
					$chatMsgList.append(_tpl);

					wchat_ToBottom();
				}
			});
			// 发送信息
			var $chatMsgList = $("#J__chatMsgList");
			function isEmpty(){
				var html = $editor.html();
				html = html.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
				html = html.replace(/<[^img].*?>/ig, "");
				html = html.replace(/&nbsp;/ig, "");
				
				return html.replace(/\r\n|\n|\r/, "").replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, "") == "";
			}
			$(".J__wchatSubmit").on("click", function(){
				// 判断内容是否为空
				if(isEmpty()) return;
//|&
				var html = $editor.html();
				console.log(html);
				
				var reg =/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\&|-|\;|\:)+)/g;
				//var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|-)+)/g;
				html = html.replace(reg, "<a href='$1$2'>$1$2</a>");

				console.log(html);
				//记录消息
				$.post("/chat/group", { id: ljc_id, content: html,fid:ljc_fid },
				function(data){
					//消息回调
					if(data=="s1"){
						Tmsg('聊天室当前禁言不能发言',3);
						return false;
					}else if(data=="s2"){
						Tmsg('您已经被踢出此聊天室',3);
						window.location.href = '/qchat';
						return false;
					}else if(data=="s9"){
					    Tmsg('您发言过于频繁了',3);
						return false;
					}
					});
				var msgTpl = [
					'<li class="me">\
						<div class="content">\
							<p class="author">'+ ljc_name +'</p>\
							<div class="msg">'+ html +'</div>\
						</div>\
						<a class="avatar" href="/chat/fdinfo"><img src="'+ ljc_tx +'" /></a>\
					</li>'
				].join("");
				$chatMsgList.append(msgTpl);

				// 清空聊天框并获取焦点（处理输入法和表情 - 聚焦）
				if(!$(".wc__choose-panel").is(":hidden")){
					$editor.html("");
				}else{
					$editor.html("").focus().trigger("click");
				}
				wchat_ToBottom();
			});

			// ...长按弹出菜单
// 			$("#J__chatMsgList").on("longTap", "li .msg", function(e){
// 				var that = $(this), menuTpl, menuNode = $("<div class='wc__chatTapMenu animated anim-fadeIn'></div>");
// 				that.addClass("taped");
// 				that.parents("li").siblings().find(".msg").removeClass("taped");
// 				var isRevoke = that.parents("li").hasClass("me");
// 				var _revoke = isRevoke ? "<a href='#'><i class='ico i4'></i>撤回</a>" : "";
				
// 				if(that.hasClass("picture")){
// 					console.log("图片长按");
// 					menuTpl = "<div class='menu menu-picture'><a href='#'><i class='ico i1'></i>复制</a><a href='#'><i class='ico i2'></i>收藏</a><a href='#'><i class='ico i3'></i>另存为</a>"+ _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
// 				}else if(that.hasClass("video")){
// 					console.log("视频长按");
// 					menuTpl = "<div class='menu menu-video'><a href='#'><i class='ico i3'></i>另存为</a>" + _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
// 				}else{
// 					console.log("文字长按");
// 					menuTpl = "<div class='menu menu-text'><a href='#'><i class='ico i1'></i>复制</a><a href='#'><i class='ico i2'></i>收藏</a>" + _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
// 				}

// 				if(!$(".wc__chatTapMenu").length){
// 					$(".wc__chatMsg-panel").append(menuNode.html(menuTpl));
// 					autoPos();
// 				}else{
// 					$(".wc__chatTapMenu").hide().html(menuTpl).fadeIn(250);
// 					autoPos();
// 				}

// 				function autoPos(){
// 					console.log(that.position().top)
// 					var _other = that.parents("li").hasClass("others");
// 					$(".wc__chatTapMenu").css({
// 						position: "absolute",
// 						left: that.position().left + parseInt(that.css("marginLeft")) + (_other ? 0 : that.outerWidth() - $(".wc__chatTapMenu").outerWidth()),
// 						top: that.position().top - $(".wc__chatTapMenu").outerHeight() - 8
// 					});
// 				}
// 			});
			// ...销毁长按弹窗
			$(".wc__chatMsg-panel").on("scroll", function(){
				$(".wc__chatTapMenu").hide();
				$(this).find("li .msg").removeClass("taped");
			});

			// 定位光标尾部
			function posCursorEnd(obj) {
				if (window.getSelection) {//ie11 10 9 ff safari
					obj.focus(); //解决ff不获取焦点无法定位问题
					var range = window.getSelection();//创建range
					range.selectAllChildren(obj);//range 选择obj下所有子内容
					range.collapseToEnd();//光标移至最后
				}
				else if (document.selection) {//ie10 9 8 7 6 5
					var range = document.selection.createRange();//创建选择对象
					//var range = document.body.createTextRange();
					range.moveToElementText(obj);//range定位到obj
					range.collapse(false);//光标移至最后
					range.select();
				}
			}

			// 长按@聊友longTap
			$("#J__chatMsgList").on("click", "li .avatar", function(e){
				var author = $(this).siblings(".content").find(".slt").text();
				var atidx = wcPop({
					skin: 'actionsheet',
					content: '<span style="color:#aaa;">'+author+'</span>',
					anim: 'footer',
					shadeClose: true,
					
					btns: [
						{
							text: '@他/她',
							style: 'color:#00c1de;',
							onTap() {
								$(".wc__choose-panel").hide();
								// @内容
								var _html = '<em class="onez-at" data-at="1233" data-at-uname="@'+author+'">@'+author+'</em>&nbsp;';
								$editor.html("").append(_html);
								
								posCursorEnd(_editor);
								wcPop.close(atidx);
							}
						},
						{
							text: '取消',
							onTap() {
								wcPop.close(atidx);
							}
						}
					]
				});
			});
		});
